/* $Id: log.h 16 2006-12-16 15:37:49Z judas $ */

#ifndef NIH_LOG_H
#define NIH_LOG_H 1

#include <nih/cdefs.h>

BEGIN_C_DECLS

#include <stdarg.h>
#if HAVE_STDBOOL_H
#include <stdbool.h>
#endif /* HAVE_STDBOOL_H */
#include <stdio.h>
#include <stdlib.h>
#if HAVE_SYSLOG_H
#include <syslog.h>
#endif /* HAVE_SYSLOG_H */

/* TODO: com_err support! */

extern int nih_log_sysprio;
extern int nih_log_errprio;
extern int nih_log_logprio;

extern FILE *nih_log_file;

enum {
        NIH_LOG_OFF = 0,
        NIH_LOG_ON = 1,
        NIH_LOG_COLOR = 3
};

extern int nih_log_errtype;
extern bool nih_log_ext;

extern int nih_log_full(const char *file, const char *fnc, int line,
                        int sysprio, int errprio, int logprio,
                        FILE *logfile, int errtype, bool ext, int errn,
                        int prio, const char *format, va_list ap);

extern int nih_log_dflt(const char *file, const char *fnc, int line, int errn,
                        int prio, const char *format, ...)
           __attribute__((format(printf, 6, 7)));

extern inline int nih_log_dflt(const char *file, const char *fnc, int line,
                               int errn, int prio, const char *format, ...)
{
        va_list args;
        int res;

        /* TODO: check priorities before calling function */

        va_start(args, format);

        res = nih_log_full(file, fnc, line, nih_log_sysprio, nih_log_errprio,
                           nih_log_logprio, nih_log_file, nih_log_errtype,
                           nih_log_ext, errn, prio, format, args);
        va_end(args);

        return res;
}

#define nih_log(prio, ...) nih_log_dflt(__FILE__, __func__, __LINE__, \
                                        0, (prio), __VA_ARGS__)

#define nih_err(prio, ...) nih_log_dflt(__FILE__, __func__, __LINE__, \
                                        errno, (prio), __VA_ARGS__)

#ifdef NDEBUG
#define nih_debug(...) /* void */
#else /* NDEBUG */
#define nih_debug(...) nih_log_dflt(__FILE__, __func__, __LINE__, \
                                    0, LOG_DEBUG, __VA_ARGS__)
#endif /* NDEBUG */

END_C_DECLS

#endif /* NIH_LOG_H */

